/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-02 19:40:58
 * Copyright (c) 2015- joysuch-版权所有
 */
package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpEnterpriseInfoAddBean;
import com.joysuch.wwyt.bp.bean.BpEnterpriseInfoBean;
import com.joysuch.wwyt.bp.entity.BpEnterpriseInfo;
import com.joysuch.wwyt.bp.repository.BpEnterpriseInfoDao;
import com.joysuch.wwyt.bp.service.BpEnterpriseInfoService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.constant.Constants;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/enterprise")
@Api(tags = "企业信息")
public class BpEnterpriseInfoController {

    private static final Logger logger = LoggerFactory.getLogger(BpEnterpriseInfoController.class);

    @Autowired
    private BpEnterpriseInfoService bpEnterpriseInfoService;
    @Autowired
    private BpEnterpriseInfoDao bpEnterpriseInfoDao;
    @Autowired
    private BaseConfigService baseConfigService;

    @PostMapping("add")
    public ResultBean add(@RequestBody BpEnterpriseInfo entity) {
        try {
            bpEnterpriseInfoService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }


    @PostMapping("update")
    public Object update(@RequestBody @Valid BpEnterpriseInfoAddBean entity) {
        try {
            bpEnterpriseInfoService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @GetMapping("getCurrUserCount")
    public ResultBean getCurrUserCount() {
        return ResultBean.success(bpEnterpriseInfoService.getCurUserCount());
    }

    @GetMapping("page")
    public ResultBean page(Pageable page) {
        try {
            List<BpEnterpriseInfoBean> byPage = bpEnterpriseInfoService.findByPage(page);
            return ResultBean.wrapSuccessfulResult(byPage);
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @GetMapping("getEnterpriseInfo")
    @ApiOperation("获取当企业")
    public ResultBean getEnterpriseInfo() {
        List<SimpleSelectListBean> list = bpEnterpriseInfoService.getEnterpriseInfo();
        return ResultBean.wrapSuccessfulResult(list);
    }


    @PostMapping("delete")
    public ResultBean delete(Long id) {
        try {
            bpEnterpriseInfoService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("/companyList")
    @ResponseBody
    public ResultBean companyList() {
        return ResultBean.success(bpEnterpriseInfoDao.findAll());
    }

    @GetMapping("/loginType")
    @ResponseBody
    @ApiOperation("账号登录方式，不配置默认密码登录")
    public ResultBean loginType(Long tenentId, String companyCode) {
        Assert.isTrue(tenentId != null && companyCode != null, "tenentId and comanyCode cannot be null");
        BaseConfig config = baseConfigService.findConfig(tenentId, companyCode, BaseConfigCodes.LOGIN_TYPE);
        if (config == null) {
            return ResultBean.success(Constants.LOGIN_TYPE_PASSWORD);
        }
        String value = config.getValue();
        if (Constants.LOGIN_TYPE_PASSWORD.equalsIgnoreCase(value) || Constants.LOGIN_TYPE_SMS.equalsIgnoreCase(value)) {
            return ResultBean.success(value);
        }
        return ResultBean.success(Constants.LOGIN_TYPE_PASSWORD);
    }

}
